package com.ruoyi.task;

import com.ruoyi.common.core.domain.entity.SysArea;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysAreaService;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;

/**
 * 定时任务
 * @author lmm
 */
@Component("syncSysAreaTask")
public class SyncSysAreaTask {
    public static ISysAreaService areaService = SpringUtils.getBean(ISysAreaService.class);
    public static RedisCache redisCache = SpringUtils.getBean(RedisCache.class);

    public void syncSysAreaTask() {
        Collection<String> keys = redisCache.keys(SysArea.CACHE_KEY_SYS_AREA_PROVINCE_CITY + "*");
        redisCache.deleteObject(keys);
        SysArea areaParam = new SysArea();
        areaParam.setStatus("0");
        List<SysArea> list = areaService.selectAreaList(areaParam);
        redisCache.setCacheObject(SysArea.CACHE_KEY_SYS_AREA,list);
        list.stream().forEach(e -> {
            redisCache.setCacheObject(SysArea.CACHE_KEY_SYS_AREA_ID + "_" + e.getDeptId(), e);
            if("1".equals(e.getRiskflag()) || "2".equals(e.getRiskflag()) || "4".equals(e.getRiskflag()) || "9".equals(e.getRiskflag())){
                redisCache.setCacheObject(SysArea.CACHE_KEY_SYS_AREA_PROVINCE_CITY + "_" + e.getProvinceName() + e.getDeptName(), e);
            }
        });

    }

}
